package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Club;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ClubModel;

/**
 * Created by Lix on 2018/10/login_bg2.
 */

public class ClubDetailActivity extends AppCompatActivity implements View.OnClickListener  {


    private TextView club_name,club_intro,club_tel,club_add,club_time,mas_name,mas_intro;
    private ImageView club_img,back,master_img;
    private Button like,btn_class,btn_video;
    private SharedPreferences sp;
    private Context mContext;
    private int cid;
    private int uid;
    private Integer integer_cid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubdetails);
        initViews();
        initEvent();
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        cid = getIntent().getIntExtra("clubid",1);
        uid = sp.getInt("uid",4);
        ClubModel clubModel =new ClubModel();
        clubModel.getClub(cid,listener);
        clubModel.isliked(uid,cid,islikelistener);

    }



    TListener<Club> listener = new TListener<Club>() {
        @Override
        public void onResponse(Club club) {
            club_name.setText(club.getClub_name());
            club_intro.setText(club.getIntro());
            club_add.setText(club.getAddress());
            club_tel.setText(Integer.toString(club.getPhone()));
            club_time.setText(club.getOpentime());
            mas_name.setText(club.getMaster_name());
            mas_intro.setText(club.getMaster_intro());
            Picasso.with(ClubDetailActivity.this)
                    .load(Server.PIC_URL_J+club.getPic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(club_img);
            Picasso.with(ClubDetailActivity.this)
                    .load(Server.PIC_URL_J+club.getMaster_pic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(master_img);


        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Success> islikelistener = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if (success.getError().equals("0")){
                like.setText("取消关注");
            }else if(success.getError().equals("1")){
                like.setText("关注");
            }
        }
        @Override
        public void onFail(String msg) {

        }
    };

    TListener<Success> likeclub = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if (success.getError().equals("0")){
                Toast.makeText(ClubDetailActivity.this,"关注成功！", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(ClubDetailActivity.this,"关注失败！", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<Success> unlikeclub = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if (success.getError().equals("0")){
                Toast.makeText(ClubDetailActivity.this,"取消关注成功！", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(ClubDetailActivity.this,"取消关注失败！", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };



    private void initViews(){
        club_name = (TextView)findViewById(R.id.club_name);
        club_intro = (TextView)findViewById(R.id.club_intro);
        club_tel = (TextView)findViewById(R.id.club_tel);
        club_add = (TextView)findViewById(R.id.club_address);
        club_time = (TextView)findViewById(R.id.club_time);
        mas_name = (TextView)findViewById(R.id.club_master_name);
        mas_intro = (TextView)findViewById(R.id.club_master_intro);

        club_img = (ImageView)findViewById(R.id.club_img);
        back = (ImageView) findViewById(R.id.club_back);
        master_img = (ImageView) findViewById(R.id.club_master_img);

        like = (Button)findViewById(R.id.btn_club_like);
        btn_class = (Button)findViewById(R.id.club_class_btn);
        btn_video = (Button)findViewById(R.id.club_video_btn);
    }

    private void initEvent(){
        back.setOnClickListener(this);
        like.setOnClickListener(this);
        btn_class.setOnClickListener(this);
        btn_video.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        ClubModel clubModel =new ClubModel();
        switch (v.getId()){
            case R.id.club_back:
                finish();
                break;
            case  R.id.club_class_btn:
                Intent intent = new Intent(ClubDetailActivity.this,ClubClassActivity.class);
                intent.putExtra("clubid",cid);
                startActivity(intent);
                break;
            case  R.id.club_video_btn:
                Intent intent1 = new Intent(ClubDetailActivity.this,ClubVideoActivity.class);
                intent1.putExtra("clubid",cid);
                startActivity(intent1);
                break;
            case R.id.btn_club_like:
                if(like.getText().equals("关注")){
                    clubModel.likeclub(cid,uid,likeclub);
                    like.setText("取消关注");
                }else if(like.getText().equals("取消关注")){
                    clubModel.unlikeclub(cid,uid,unlikeclub);
                    like.setText("关注");
                }
                break;

        }

    }
}
